/**
 * gvSIG. Desktop Geographic Information System.
 *
 * Copyright (C) 2007-2012 gvSIG Association.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 *
 * For any additional information, do not hesitate to contact us
 * at info AT gvsig.com, or visit our website www.gvsig.com.
 */
package org.gvsig.titleblock.app.mainplugin;

import org.gvsig.andami.IconThemeHelper;
import org.gvsig.andami.plugins.Extension;
import org.gvsig.andami.plugins.IExtension;
import org.gvsig.andami.ui.mdiManager.IWindow;
import org.gvsig.app.ApplicationLocator;
import org.gvsig.app.ApplicationManager;
import org.gvsig.app.project.documents.layout.gui.LayoutPanel;
import org.gvsig.titleblock.ITBManager;
import org.gvsig.titleblock.impl.TBManager;
import org.gvsig.titleblock.impl.TBToFFrame;
import org.gvsig.titleblock.swing.ITBSwingManager;
import org.gvsig.titleblock.swing.impl.JOpenTBPanel;
import org.gvsig.titleblock.swing.impl.TBSwingManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Andami extension to show TitleBlock in the application.
 * 
 * @author gvSIG Team
 * @version $Id$
 */
public class OpenTitleBlockExtension extends Extension  {
    
    private static final Logger logger =
        LoggerFactory.getLogger(OpenTitleBlockExtension.class);

    private static final String ACTION_OPEN = "Open_Title_Block";

    private ITBManager manager;
    private LayoutPanel layout;
    private ApplicationManager applicationManager= null;
    
    /**
     * Register an icon for the extension
     */
    public void initialize() {
    	IconThemeHelper.registerIcon(null, "open-title-block", this);
    }

    /**
     * Execute the actions associated to this extension. 
     */
    public void execute(String actionCommand) {
	    if( ACTION_OPEN.equalsIgnoreCase(actionCommand) ) {
	    	manager = new TBManager();
		    TBToFFrame tbToFFrame = new TBToFFrame(manager);
		    manager.initialize(null, tbToFFrame, layout);
	    	
		    ITBSwingManager swingManager = new TBSwingManager();
		    JOpenTBPanel panel = (JOpenTBPanel)swingManager.createJOpenTBPanel(manager);
		    
		    applicationManager = ApplicationLocator.getManager();
		    applicationManager.getUIManager().addWindow(panel);
	    }
    }

    /**
     * Check if tools of this extension are enabled.
     */
    public boolean isEnabled() {
    	//
    	// By default the tool is always enabled
    	//
        return true;
    	
    }

    /**
     * Check if tools of this extension are visible.
     */
    public boolean isVisible() {
    	//
    	// The tool is visible only when our view is active
    	//
    	
    	IWindow v = ApplicationLocator.getManager().getUIManager().getActiveWindow();
    	
    	if(v instanceof LayoutPanel){
    		layout = (LayoutPanel) v;
    		return true; 
    	} else 
    		return false;
    }

    /**
     * Check if an extension is enabled.
     */
    public boolean isEnabled(IExtension extension) {
       return true;
    }
      
}
